package com.qf.ithub.movie_demo01.controller;

import com.qf.ithub.common.result.R;
import com.qf.ithub.common.result.ResponseEnum;
import com.qf.ithub.movie_demo01.common.aop.CheckLogin;
import com.qf.ithub.movie_demo01.entity.Role;
import com.qf.ithub.movie_demo01.entity.dto.ActionDTO;
import com.qf.ithub.movie_demo01.entity.dto.GrantDTO;
import com.qf.ithub.movie_demo01.entity.dto.MenuDTO;
import com.qf.ithub.movie_demo01.service.impl.RoleServiceImpl;
import com.qf.ithub.movie_demo01.service.impl.RrightServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : xixi
 * @Title :
 * @date :  2021-03-18 21:02
 */
@RestController
@RequestMapping("/admin/role")
@Api(description = "管理员使用的角色接口")
@CrossOrigin("*")
@Slf4j
public class RoleController {

    @Autowired
    private RrightServiceImpl rrightService;
    @Autowired
    private RoleServiceImpl roleService;

    @GetMapping("/menus")
    @CheckLogin
    public R findMenusByRole(HttpServletRequest request){
        System.out.println(request.getAttribute("roleid"));
        Integer roleid = Integer.parseInt(request.getAttribute("roleid").toString());
        List<MenuDTO> menuDTOByRoleid = rrightService.findMenuDTOByRoleid(roleid);
        return new R(ResponseEnum.SUCCESS,menuDTOByRoleid);
    }

    /*
    * 查询所有的角色
    * */
    @GetMapping("/roles")
    public R getRoles(){
        List<Role> role = roleService.getRole();
        return new R(ResponseEnum.SUCCESS,role);
    }

    @PostMapping("/addrole")
    @ApiOperation("添加角色")
    public R addRole(@RequestBody  Role role){
        roleService.addRole(role);
        return new R(ResponseEnum.SUCCESS,null);
    }

    /**
     * 查询所有的动作权限
     * @return
     */
    @GetMapping("/all/actionRight")
    public R selectAllActionRightByParentid(){
        List<ActionDTO> actionDTOS = roleService.selectAllActionRightByParentid();
        return new R(ResponseEnum.SUCCESS,actionDTOS);
    }
    /**

     * 获得角色相对应的除了父节点的动作权限
     */
    @GetMapping("/actions/{roleid}")
    public  R  selectActionRightByRolNoParent(Integer roleid){
        List<Integer> actions = roleService.selectActionRightByRolNoParent(roleid);
        return  new R(ResponseEnum.SUCCESS,actions);
    }

    /**
     * 给指定的其角色赋予动作权限
     */
    @PostMapping("/grant/action")
    public R grantMenuRight(@RequestBody GrantDTO grantDTO){
        roleService.grantActionRight(grantDTO);
        return new R(ResponseEnum.SUCCESS,null);
    }
}
